package com.virtualtrainer_01.dao;

import com.virtualtrainer_01.model.Client;
import com.virtualtrainer_01.model.Model;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

public class ClientDB extends SQLiteDB {
	public ClientDB(Context c) {
		super(c, DATABASE_TABLE);
	}

	public static final String KEY_ID = "_id";
	public static final String KEY_NAME = "name";
	public static final String KEY_BIRTHDAY = "birthday";
	public static final String KEY_WEIGHT = "weight";
	public static final String KEY_HEIGHT = "height";
	public static final String KEY_GENRE = "genre";
	public static final String KEY_DISPONIBILITY = "disp";
	public static final String DATABASE_TABLE = "client";

	public long createEntry(Model entry) {
		Client c = (Client) entry;
		ContentValues cv = new ContentValues();
		cv.put(KEY_NAME, c.getName());
		cv.put(KEY_BIRTHDAY, c.getBirthday());
		cv.put(KEY_WEIGHT, c.getWeight());
		cv.put(KEY_HEIGHT, c.getHeigth());
		cv.put(KEY_GENRE, c.isGenre());
		cv.put(KEY_DISPONIBILITY, c.getDisponibility());
		return ourDatabase.insert(DATABASE_TABLE, null, cv);
	}

	public String getData() {
		String[] columns = new String[] { KEY_ID, KEY_NAME, KEY_BIRTHDAY,
				KEY_WEIGHT, KEY_HEIGHT, KEY_GENRE, KEY_DISPONIBILITY };
		Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null,
				null, null);
		String result = "";

		int iRow = c.getColumnIndex(KEY_ID);
		int iName = c.getColumnIndex(KEY_NAME);
		int iBirth = c.getColumnIndex(KEY_BIRTHDAY);
		int iWeight = c.getColumnIndex(KEY_WEIGHT);
		int iHeight = c.getColumnIndex(KEY_HEIGHT);
		int iGenre = c.getColumnIndex(KEY_GENRE);
		int iDisp = c.getColumnIndex(KEY_DISPONIBILITY);

		for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
			result = result + c.getString(iRow) + " " + c.getString(iName)
					+ " " + c.getString(iBirth) + " " + c.getString(iWeight)
					+ " " + c.getString(iHeight) + " " + c.getString(iGenre)
					+ " " + c.getString(iDisp) + "\n";
		return result;
	}

	public String getName() {
		String[] columns = new String[] { KEY_NAME };
		Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null,
				null, null);
		String result = null;

		int iName = c.getColumnIndex(KEY_NAME);

		for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
			result = c.getString(iName);
		return result;
	}

	public Client getClient() {

		String[] columns = new String[] { KEY_NAME, KEY_BIRTHDAY,
				KEY_WEIGHT, KEY_HEIGHT, KEY_GENRE, KEY_DISPONIBILITY };
		Cursor c = ourDatabase.query(DATABASE_TABLE, columns, null, null, null,
				null, null);
		
		int iName = c.getColumnIndex(KEY_NAME);
		int iBirth = c.getColumnIndex(KEY_BIRTHDAY);
		int iWeight = c.getColumnIndex(KEY_WEIGHT);
		int iHeight = c.getColumnIndex(KEY_HEIGHT);
		int iGenre = c.getColumnIndex(KEY_GENRE);
		int iDisp = c.getColumnIndex(KEY_DISPONIBILITY);

		Client lastClient = new Client();
		for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext())
			lastClient = new Client(c.getString(iName), c.getString(iBirth),
					Double.parseDouble(c.getString(iWeight)),
					Double.parseDouble(c.getString(iHeight)),
					Boolean.parseBoolean(c.getString(iGenre)),
					Integer.parseInt(c.getString(iDisp)));
		return lastClient;
	}
}
